<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/ArtifactHasTagDAOInterface.php';
include_once dirname(__FILE__).'/../../object/ArtifactHasTag.php';
include_once dirname(__FILE__).'/RequirementDAO.php';
include_once dirname(__FILE__).'/TagDAO.php';

class ArtifactHasTagDAO implements ArtifactHasTagDAOInterface {
	
	public static $TABLE_NAME = 'ARTIFACT_HAS_TAG';
	public static $COL_ARTIFACT_PK = 'ARTIFACT_PK';
	public static $COL_TAG_PK = 'TAG_PK';
	
	public function __construct() {
		
	}
	
	/**
	 * Them mot dong vao bang 'ARTIFACT_HAS_TAG'.
	 * Tra ve so dong da them 
	 * @see ArtifactHasTagDAOInterface::insert()
	 * @param ArtifactHasTag $artifactHasTag
	 * @return int 
	 */
	public function insert($artifactHasTag) {
		$table = self::$TABLE_NAME;
		$colArtifactPK = self::$COL_ARTIFACT_PK;
		$colTagPK = self::$COL_TAG_PK;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        INSERT INTO $table
        	($colArtifactPK, $colTagPK)
                VALUES (:artifact_pk, :tag_pk)
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':artifact_pk' => $artifactHasTag->getArtifactPK(),
				':tag_pk' => $artifactHasTag->getTagPK()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Xoa mot dong co PK tuong ung.
	 * Tra ve so dong da xoa.
	 * @see ArtifactHasTagDAOInterface::delete()
	 * @param ArtifactHasTag $artifactHasTag
	 * @return int
	 */
	public function delete($artifactHasTag) {
		$table = self::$TABLE_NAME;
		$colArtifactPK = self::$COL_ARTIFACT_PK;
		$colTagPK = self::$COL_TAG_PK;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        DELETE FROM $table
        	WHERE $colArtifactPK = :artifact_pk
        	AND $colTagPK = :tag_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':artifact_pk' => $artifactHasTag->getArtifactPK(),
				':tag_pk' => $artifactHasTag->getTagPK()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Tra ve cac Tag thuoc ve Artifact.
	 * @see ArtifactHasTagDAOInterface::getTagByArtifact()
	 * @param Artifact $artifact
	 * @return Tag (array)
	 */
	public function getTagByArtifact($artifact) {
		$table = self::$TABLE_NAME;
		$colArtifactPK = self::$COL_ARTIFACT_PK;
		$colTagPK = self::$COL_TAG_PK;
		
		$tableTag = TagDAO::$TABLE_NAME;
		$colTagName = TagDAO::$COL_TAG_NAME;
		$colTagDes = TagDAO::$COL_TAG_DESCRIPTION;
		
		
		$tableReq = RequirementDAO::$TABLE_NAME;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        SELECT a.$colTagPK, a.$colTagName, a.$colTagDes
        FROM $tableTag a, $tableReq b, $table c
        WHERE 	b.$colArtifactPK = c.$colArtifactPK
        	AND	a.$colTagPK = c.$colTagPK
        	AND	b.$colArtifactPK = :artifact_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':artifact_pk' => $artifact->getArtifactPK()
		);
		
		$tags = array();
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			foreach ($sql->fetchAll() as $tag) {
				$tags[] = new Tag($tag["$colTagPK"], $tag["$colTagName"],
						$tag["$colTagDes"]);
			}
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return $tags;
	}
}